/**
 * Main Menu
 * The main operating area of the program, central hub of the blogger desktop.
 * Yeah, it's kinda important.
 */
package bloggerdesktop;

import com.google.gdata.client.*;
//import com.google.gdata.client.calendar.*;
import com.google.gdata.data.*;
import com.google.gdata.data.extensions.*;
import com.google.gdata.util.*;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JEditorPane;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author  Paul Schroeder, Christopher Kruse
 */
public class MainMenu extends javax.swing.JFrame {

    final boolean EDITING = true;
    final boolean CREATING = false;
    static Person defaultPerson;
    static GoogleService myService = new GoogleService("blogger", "BloggerDesktop");
    static Login login_l;
    GoogleDataHandler gdh;
    String currentPost = "";
    String currentBlog = "";

    /** Creates new form MainMenu */
    public MainMenu() {
        initComponents();
        this.setTitle("Blogger Desktop");
        gdh = new GoogleDataHandler(myService);
        populateBlogList(new java.awt.event.ActionEvent(this, WIDTH, "Because I said so"));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        menuBar1 = new java.awt.MenuBar();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        viewPostList = new javax.swing.JList();
        viewPostButton = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        editPostList = new javax.swing.JList();
        editPostButton = new javax.swing.JToggleButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        titleBox = new javax.swing.JTextField();
        CreateButton = new javax.swing.JToggleButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu2 = new javax.swing.JMenu();
        jScrollPane2 = new javax.swing.JScrollPane();
        blogList = new javax.swing.JList();
        populateList = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        javax.swing.JButton jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jScrollPane3.setViewportView(viewPostList);

        viewPostButton.setText("View Post");
        viewPostButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                getPostData(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(560, Short.MAX_VALUE)
                .add(viewPostButton)
                .addContainerGap())
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 587, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(57, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 211, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 29, Short.MAX_VALUE)
                .add(viewPostButton)
                .addContainerGap())
        );

        jTabbedPane1.addTab("View Post", jPanel1);

        jScrollPane1.setViewportView(editPostList);

        editPostButton.setText("Edit Post");
        editPostButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editSelectedPost(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 587, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(57, Short.MAX_VALUE))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(564, Short.MAX_VALUE)
                .add(editPostButton)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 211, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 29, Short.MAX_VALUE)
                .add(editPostButton)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Edit Existing Post", jPanel2);

        jLabel2.setText("Title:");

        CreateButton.setText("Create");
        CreateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createPost(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel2)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(titleBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 302, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(304, Short.MAX_VALUE))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(575, Short.MAX_VALUE)
                .add(CreateButton)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(titleBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 222, Short.MAX_VALUE)
                .add(CreateButton)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Create New Post", jPanel3);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        jScrollPane2.setViewportView(blogList);

        populateList.setText("Repopulate");
        populateList.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                populateBlogList(evt);
            }
        });

        jLabel1.setText("Select Blog:");

        jButton1.setText("Select Blog");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                populatePostLists(evt);
            }
        });

        jButton2.setText("Quit");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                main_quit(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 587, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel1)
                    .add(layout.createSequentialGroup()
                        .add(populateList)
                        .add(388, 388, 388)
                        .add(jButton1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButton2))
                    .add(jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 661, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jTabbedPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 661, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 184, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton1)
                    .add(jButton2)
                    .add(populateList))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 7, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jTabbedPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 314, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void editSelectedPost(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editSelectedPost
        try {
            String blog_id = gdh.blogIDFromName(blogList.getSelectedValue());
            String post_id = gdh.postIDFromName(editPostList.getSelectedValue());
            Entry en = gdh.retrievePostData(blog_id, post_id);
            TextEditor ted;
            ted = new TextEditor(en, gdh, EDITING,(String) blogList.getSelectedValue());
            ted.setVisible(true);
            editPostButton.setSelected(false);
        } catch (IOException ex) {
            javax.swing.JOptionPane.showMessageDialog(null, "Post not retrieved.");
        }

    }//GEN-LAST:event_editSelectedPost

    private void populateBlogList(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_populateBlogList
        String[] cheese = gdh.listBlogsTitles();
        blogList.setListData(cheese);
}//GEN-LAST:event_populateBlogList

    private void main_quit(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_main_quit
        // TODO add your handling code here:
        int result = javax.swing.JOptionPane.showConfirmDialog(this, "Are you sure you want to quit?");
        if (result == javax.swing.JOptionPane.YES_OPTION) {
            System.exit(0);
        } else {
            ;
        }
    }//GEN-LAST:event_main_quit

    private void populatePostLists(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_populatePostLists
        try {
            String id = gdh.blogIDFromName(blogList.getSelectedValue());
            gdh.retrievePosts(id);
            String[] posts = gdh.listPostTitles();
            viewPostList.setListData(posts);
            editPostList.setListData(posts);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "An error occurred.\n" + e.getMessage() + "\n" + e.getStackTrace());
        }
    }//GEN-LAST:event_populatePostLists

    private void getPostData(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_getPostData
        try {
            String blog_id = gdh.blogIDFromName(blogList.getSelectedValue());
            String post_id = gdh.postIDFromName(viewPostList.getSelectedValue());
            Entry en = gdh.retrievePostData(blog_id, post_id);
            TextViewer tv = new TextViewer(en, gdh, blog_id, post_id);
            tv.setVisible(true);
        } catch (IOException e) {
            javax.swing.JOptionPane.showMessageDialog(null, "Post not retrieved.");
        }
    }//GEN-LAST:event_getPostData

    private void createPost(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createPost
        try {
            Entry en = gdh.createPost(titleBox.getText(),defaultPerson);
            String blogID = gdh.blogIDFromName((String) blogList.getSelectedValue());
            TextEditor ted = new TextEditor(en, gdh, CREATING,blogID);
            ted.setVisible(true);
        } catch (ServiceException ex) {//GEN-LAST:event_createPost
            Logger.getLogger(MainMenu.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(MainMenu.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @SuppressWarnings("static-access")
    public static void mainStart() {
        login_l.ine.setVisible(false);
        login_l.ine.dispose();
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MainMenu().setVisible(true);

            }
        });
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        //Set native look and feel for the GUI
        String nativeLF = UIManager.getSystemLookAndFeelClassName();
        // Set the look and feel
        try {
            UIManager.setLookAndFeel(nativeLF);
        } catch (Exception e) {
            ; // Silently fail, and go with the default l&f.
        }
        login_l = new Login();
        login_l.start();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton CreateButton;
    private javax.swing.JList blogList;
    private javax.swing.JToggleButton editPostButton;
    private javax.swing.JList editPostList;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private java.awt.MenuBar menuBar1;
    private javax.swing.JButton populateList;
    private javax.swing.JTextField titleBox;
    private javax.swing.JButton viewPostButton;
    private javax.swing.JList viewPostList;
    // End of variables declaration//GEN-END:variables
}
